package com.memoir.service.impl.view;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.memoir.model.entity.image.MSpaceEntity;
import com.memoir.model.entity.image.MSpaceTagRelationEntity;
import com.memoir.mapper.image.MSpaceMapper;
import com.memoir.mapper.image.MSpaceTagRelationMapper;
import com.memoir.service.view.ImageViewFontService;
import com.memoir.model.vo.rep.font.QueryViewImagesRepVo;
import com.memoir.model.vo.req.font.QueryViewImagesReqVo;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@AllArgsConstructor
@Component
public class ImageViewFontServiceImpl implements ImageViewFontService {

   // LogUtils LOG = LogUtils.class.

    private final MSpaceMapper mSpaceMapper;

    private final MSpaceTagRelationMapper mSpaceTagRelationMapper;

    @Override
    public List<QueryViewImagesRepVo> queryImageSpaceList(QueryViewImagesReqVo param) {
        LambdaQueryWrapper<MSpaceEntity> condition = new LambdaQueryWrapper<MSpaceEntity>();
        if(StringUtils.isNoneBlank(param.getImageTypeId())){
            condition.eq(MSpaceEntity::getSpaceTypeId, param.getImageTypeId());
        }
        if(StringUtils.isNoneBlank(param.getSpaceName())){
            condition.like(MSpaceEntity::getSpaceName, param.getSpaceName());
        }
        //获取空间配置的数据
        List<MSpaceEntity>  spaceEntityList = mSpaceMapper.selectList(condition);
        if (CollectionUtils.isEmpty(spaceEntityList)) {
            return null;
        }
        //TODO 标签数据读取缓存
        //获取标签
        List<String> sapceIdList = spaceEntityList.stream().map(item->item.getSpaceId()).collect(Collectors.toList());
        LambdaQueryWrapper<MSpaceTagRelationEntity> paceTagCondition = new LambdaQueryWrapper<>();
        paceTagCondition.in(MSpaceTagRelationEntity::getSpaceId, sapceIdList);
        List<MSpaceTagRelationEntity> relationList = mSpaceTagRelationMapper.selectList(paceTagCondition);
        Map<String,List<MSpaceTagRelationEntity>> spaceTagMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(relationList)) {
            spaceTagMap.putAll(relationList.stream().collect(Collectors.groupingBy(MSpaceTagRelationEntity::getSpaceId)));
        }
        List<QueryViewImagesRepVo> returnData = buildSpaceDataList(param,spaceEntityList,spaceTagMap);
        return returnData;
    }

    /**
     * 处理查询空间列表的逻辑
     * @param param 参数
     * @param spaceEntityList 空间数据
     * @param spaceTagMap 标签数据
     * @return 返回结果
     */
    private List<QueryViewImagesRepVo> buildSpaceDataList(QueryViewImagesReqVo param, List<MSpaceEntity> spaceEntityList, Map<String, List<MSpaceTagRelationEntity>> spaceTagMap) {
        List<QueryViewImagesRepVo> returnDataList = new ArrayList<>();
        //转换空间的实体为map结构
        Map<String,QueryViewImagesRepVo> spaceEntityMap = new HashMap<>();
        for(MSpaceEntity spaceEntity : spaceEntityList){
            QueryViewImagesRepVo data = new QueryViewImagesRepVo();
            BeanUtils.copyProperties(spaceEntity,data);
            spaceEntityMap.put(spaceEntity.getSpaceId(), data);
        }
        //如果没有传递标签参数
        if(CollectionUtils.isEmpty(param.getSpaceTagId())){
             returnDataList.addAll(spaceEntityMap.values().stream().toList());
        }else{
            List<String> spaaceIdList = new ArrayList<>();
            for(String key : spaceTagMap.keySet()){
                if(param.getSpaceTagId().contains(key)){
                    //获取当前的tag对象的数据
                    List<MSpaceTagRelationEntity> currentRelationList = spaceTagMap.get(key);
                    spaaceIdList.addAll(currentRelationList.stream().map(item->item.getSpaceId()).collect(Collectors.toList()));
                }
            }
            //进行筛选对应的空间数据
            for(String key : spaceEntityMap.keySet()){
                if(spaaceIdList.contains(key)){
                    returnDataList.add(spaceEntityMap.get(key));
                }
            }
        }
        return returnDataList;
    }
}
